﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;
using System.Text;

public partial class School_ExamDetail_CreateEdit : System.Web.UI.Page
{
    private void BindExam()
    {
        ddlExamList.Items.Clear();
        ddlExamList.Items.Add(new ListItem("Select", "-1"));

        ExamList _ExamList = new ExamList();
        ExamDB _ExamDB = new ExamDB();
        _ExamList = _ExamDB.GetExamList(Convert.ToInt32(hidCompanyId.Value));
        if (_ExamList != null && _ExamList.Count > 0)
        {
            foreach (Exam _cd in _ExamList)
            {
                ddlExamList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void BindCourseSubject()
    {
        ddlCourseSubjectList.Items.Clear();
        ddlCourseSubjectList.Items.Add(new ListItem("Select", "-1"));

        CourseSubjectList _CourseSubjectList = new CourseSubjectList();
        CourseSubjectDB _CourseSubjectDB = new CourseSubjectDB();
        _CourseSubjectList = _CourseSubjectDB.GetCourseSubjectList(Convert.ToInt32(hidCompanyId.Value));
        if (_CourseSubjectList != null && _CourseSubjectList.Count > 0)
        {
            foreach (CourseSubject _cd in _CourseSubjectList)
            {
                ddlCourseSubjectList.Items.Add(new ListItem(_cd.SubjectName , _cd.Id.ToString()));
            }
        }
    }

    private void ShowCourseSubject()
    {
        CourseSubject _CourseSubject = new CourseSubject();
        CourseSubjectDB _CourseSubjectDB = new CourseSubjectDB();
        _CourseSubject = _CourseSubjectDB.GetCourseSubject(Convert.ToInt32(ddlCourseSubjectList.Text));

        if (_CourseSubject != null && _CourseSubject.Id > 0)
        {
            StringBuilder _strCourseSubject = new StringBuilder();
            _strCourseSubject.Append("<table cellpadding='3' cellspacing='4'>");
            _strCourseSubject.Append("<tr><td>Course Detail:</td><td>" + _CourseSubject.CourseDetailName + "</td></tr>");
            _strCourseSubject.Append("<tr><td>Course:</td><td>" + _CourseSubject.CourseName + "</td></tr>");
            _strCourseSubject.Append("<tr><td>Description:</td><td>" + _CourseSubject.Description + "</td></tr>");
            _strCourseSubject.Append("</table>");

            lblCourseSubject.Text = _strCourseSubject.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();

                BindCourseSubject();
                BindExam();

                if (Request.Params["id"] != null)
                {
                    int _ExamDetailId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _ExamDetailId) && _ExamDetailId > 0)
                    {
                        ExamDetail _ExamDetail = new ExamDetail();
                        ExamDetailDB _ExamDetailDB = new ExamDetailDB();
                        _ExamDetail = _ExamDetailDB.GetExamDetail(_ExamDetailId);

                        if (_ExamDetail.Id > 0)
                        {
                            if (_ExamDetail.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _ExamDetail.Error.ErrorMessage;
                            }
                            else
                            {
                                hidExamDetailId.Value = _ExamDetailId.ToString();
                                txtExamDetailName.Text = _ExamDetail.Name;
                                txtDescription.Text = _ExamDetail.Description;
                                ddlCourseSubjectList.Text = _ExamDetail.CourseSubjectId.ToString();
                                ddlCourseSubjectList_SelectedIndexChanged(sender, e);
                                ddlExamList.Text = _ExamDetail.ExamId.ToString();
                                
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                ExamDetail ExamDetail = new ExamDetail();
                ExamDetailDB _ExamDetailDB = new ExamDetailDB();
                int _ExamDetailId = 0;

                if (int.TryParse(hidExamDetailId.Value, out _ExamDetailId) && _ExamDetailId > 0)
                {
                    ExamDetail = _ExamDetailDB.GetExamDetail(_ExamDetailId);
                }
                else
                {
                    ExamDetail.Id = 0;
                    ExamDetail.Error = new MyError();
                }

                if (txtDescription.Text.Trim().Length > 0)
                {
                    ExamDetail.Description = txtDescription.Text;
                }
                else
                {
                    ExamDetail.Description = string.Empty;
                }

                ExamDetail.Name = txtExamDetailName.Text;
                ExamDetail.CourseSubjectId = Convert.ToInt32(ddlCourseSubjectList.Text);
                ExamDetail.ExamId = Convert.ToInt32(ddlExamList.Text);
                ExamDetail.IsActive = false;

                _ExamDetailDB.Save(ExamDetail);

                if (ExamDetail.Error.ErrorNo > 0)
                {
                    if (ExamDetail.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = ExamDetail.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + ExamDetail.Error.ErrorLocation + ")";
                        lblMessage.Text += ExamDetail.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + ExamDetail.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

    protected void ddlCourseSubjectList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCourseSubjectList.Items.Count > 0 && ddlCourseSubjectList.SelectedIndex > 0)
        {
            ShowCourseSubject();
        }
        else
        {
            lblCourseSubject.Text = "Please select course detail.";
        }
    }
}